<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('菜单新增')"/>
</head>
<body>
<form action="" class="layui-form">
    <div class="mainBox">
        <div class="main-container">
            <div class="main-container">
                <div class="layui-form-item">
                    <label class="layui-form-label layui-form-required">父级</label>
                    <div class="layui-input-block">
                        <ul class="dtree" data-id="-1" id="selectParent" name="parentId"></ul>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label layui-form-required">名称</label>
                    <div class="layui-input-block">
                        <input autocomplete="off" class="layui-input" lay-verify="required" name="title" placeholder="菜单名称"
                               type="text">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">状态</label>
                    <div class="layui-input-block">
                        <input lay-filter="menuType" name="type" title="目录" type="radio" value="0">
                        <input checked lay-filter="menuType" name="type" title="菜单" type="radio" value="1">
<!--                        <input lay-filter="menuType" name="type" title="按钮" type="radio" value="2">-->
                    </div>
                </div>
                <div class="layui-form-item" id="menuUrlItem">
                    <label class="layui-form-label layui-form-required">路径</label>
                    <div class="layui-input-block">
                        <input autocomplete="off" class="layui-input" id="menuUrl" name="href" placeholder="菜单路径"
                               type="text" lay-verify="required">
                    </div>
                </div>
                <div class="layui-form-item" id="openTypeItem">
                    <label class="layui-form-label">打开</label>
                    <div class="layui-input-block">
                        <select id="openType" name="openType">
                            <option selected value=""></option>
                            <option value="_iframe">框架</option>
                            <option value="_blank">签页</option>
                        </select>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">图标</label>
                    <div class="layui-input-block">
                        <input class="layui-input" id="icon" lay-filter="icon" name="icon" type="text">
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="bottom">
        <div class="button-container">
            <button class="pear-btn pear-btn-primary pear-btn-sm" lay-filter="menu-save" lay-submit="" type="submit">
                <i class="layui-icon layui-icon-ok"></i>
                提交
            </button>
            <button class="pear-btn pear-btn-sm" type="reset">
                <i class="layui-icon layui-icon-refresh"></i>
                重置
            </button>
        </div>
    </div>
</form>
<th:block th:include="include :: footer"/>
<script>
    layui.use(['form', 'jquery', 'iconPicker', 'dtree', 'dictionary'], function () {
        let form = layui.form;
        let $ = layui.jquery;
        let iconPicker = layui.iconPicker;
        let dtree = layui.dtree;

        dtree.renderSelect({
            elem: "#selectParent",
            url: "/system/menu/parents",
            method: 'get',
            selectInputName: {nodeId: "parentId", context: "title"},
            skin: "layui",
            dataFormat: "list",
            response: {treeId: "id", parentId: "parentId", title: "title"},  //修改response中返回数据的定义
            selectInitVal: "0"
        });

        form.on("radio(menuType)", function () {
            if (this.value === '0') {
                $("#menuUrlItem").hide();
                $("#openTypeItem").hide();
                $("#menuUrl").val("");
                $("#menuCode").val("");
                $("#openType").val("");
            } else if (this.value === '1') {
                $("#menuUrlItem").show();
                $("#openTypeItem").show();
            } /*else if (this.value == '2') {
                $("#menuUrlItem").hide();
                $("#openTypeItem").hide();
                $("#menuUrl").val("");
                $("#openType").val("");
            }*/
        });

        form.on('submit(menu-save)', function (data) {
            data.field.icon = "layui-icon " + data.field.icon;
            $.ajax({
                url: '/system/menu/add',
                data: JSON.stringify(data.field),
                dataType: 'json',
                contentType: 'application/json',
                type: 'post',
                success: function (result) {
                    if (result.code===200) {
                        layer.msg(result.msg, {icon: 1, time: 1000}, function () {
                            parent.layer.close(parent.layer.getFrameIndex(window.name));//关闭当前页
                            parent.layui.treetable.reload("#menu-table");
                        });
                    } else {
                        layer.msg(result.msg, {icon: 2, time: 1000});
                    }
                }
            })
            return false;
        });

        iconPicker.render({
            // 选择器，推荐使用input
            elem: '#icon',
            // 数据类型：fontClass/unicode，推荐使用fontClass
            type: 'fontClass',
            // 是否开启搜索：true/false
            search: true,
            // 是否开启分页
            page: true,
            // 每页显示数量，默认12
            limit: 12,
            // 点击回调
            click: function (data) {

            }
        });
    })
</script>
<script>
</script>
</body>
</html>